package com.itany.cms.controller;

import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.ResponseCodeConstant;
import com.itany.cms.entity.Chapter;
import com.itany.cms.entity.Comment;
import com.itany.cms.exception.CourseExistException;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.service.ChapterService;
import com.itany.cms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;
    @RequestMapping("/findChapter")
    public ModelAndView findChapter(@RequestParam("id") String  id, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        Chapter chapterParam = new Chapter();
        chapterParam.setCourseId(Integer.parseInt(id));
        PageInfo<Chapter> commentPage = chapterService.findChapter(chapterParam,pageNo, pageSize);
        mav.addObject("chapterPage", commentPage);
        mav.setViewName("backend/back_courseReourceSet");
        return mav;
    }
    @RequestMapping("/findByParams")
    public ModelAndView findChapter(Chapter chapterParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Chapter> commentPage = chapterService.findByParams(chapterParam,pageNo, pageSize);
        mav.addObject("chapterPage", commentPage);
        mav.setViewName("backend/back_courseReourceSet");
        return mav;
    }
    @RequestMapping("/findComment")
    public ModelAndView findComment(@RequestParam("id") String  id, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();

        PageInfo<Comment> commentPage = chapterService.findComment(id,pageNo, pageSize);
        mav.addObject("commentPage", commentPage);
        mav.setViewName("backend/back_commentSet");
        return mav;
    }
    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            chapterService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/addChapter")
    public ResponseResult addChapter(String courseId, String title, String info,@RequestParam("file") MultipartFile file, String resourceTitle, String costType, String costNumber, String fileType) {
        ResponseResult result = new ResponseResult();
        try {
            chapterService.addChapter(courseId,title, info, file, resourceTitle,costType,costNumber,fileType);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
